﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BUS;
using DTO;
namespace FastDeal
{
    public partial class Cart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Load_GioHang();
        }
        KhuyenMai_BUS km_BUS = new KhuyenMai_BUS();
        public void Load_GioHang()
        {
            GV_DSGioHang.DataSource = TrangThaiDangNhap.LayGioHang.DsSanPhamKM_Mua;
            GV_DSGioHang.DataBind();
        }
        protected void check_payment_CheckedChanged(object sender, EventArgs e)
        {

        }
        public bool KiemTraThongTinKhachHang(ThongTinTaiKhoan ttTaiKhoan)
        {
            if (string.IsNullOrEmpty(ttTaiKhoan.Nickname) || string.IsNullOrEmpty(ttTaiKhoan.DienThoai)
               || string.IsNullOrEmpty(ttTaiKhoan.HoTen) || string.IsNullOrEmpty(ttTaiKhoan.DiaChi))
            {
                return false;
            }
            return true;
        }
        public void LapThanhToan()
        {
            ThongTinTaiKhoan tt = new ThongTinTaiKhoan();
            tt.DiaChi = txt_DiaChi.Text;
            tt.Nickname = txt_Email.Text;
            tt.DienThoai = txt_SDT.Text;
            tt.HoTen = txt_HoTen.Text;
            if (KiemTraThongTinKhachHang(tt) == false)
            {
                txt_HoTen.Text = "Nhập đầy đủ thông tin";
            }
            else
            {
  
                double tongTien = 0;
                foreach (SanPhamGioHang sp in TrangThaiDangNhap.LayGioHang.DsSanPhamKM_Mua)
                {
                    tongTien = tongTien + sp.SoLuong * sp.GiaKhuyenMai;
                }
                Session["TongTien"] = tongTien;
                HoaDon_BUS hd_BUS = new HoaDon_BUS();
                HoaDon hd = new HoaDon();
                hd.UserID = int.Parse(Session["UserID"].ToString());
                hd.NgayDatMua = DateTime.Now;
                hd.TongTien = tongTien;
                hd.GhiChu = "Gọi điện thoại trước khi giao hàng";
                hd.isDelete = 0;
                hd.DiaChiGiaoHang = txt_DiaChi.Text;
                hd.TinhTrang = 0;
                hd_BUS.Them_HoaDon(hd);
             

                List<HoaDon> dsHoaDon = new List<HoaDon>();
                dsHoaDon = hd_BUS.LayDanhSach_HoaDon();
                HoaDon hd_LayVe = dsHoaDon[dsHoaDon.Count - 1];

                ChiTiet_HoaDon_BUS ct_Bus = new ChiTiet_HoaDon_BUS();
                foreach (SanPhamGioHang sp in TrangThaiDangNhap.LayGioHang.DsSanPhamKM_Mua)
                {
                    ChiTiet_HoaDon ct = new ChiTiet_HoaDon();
                    ct.MaHoaDon = hd_LayVe.MaHoaDon;
                    ct.MaKhuyenMai = sp.MaKhuyenMai;
                    ct.GiaKhuyenMai = sp.GiaKhuyenMai;
                    ct.SoLuong = sp.SoLuong;
                    ct.TinhTrang = 0;
                    ct.isDelete = 0;
                    ct_Bus.Them_ChiTietHoaDon(ct);
                }
                TrangThaiDangNhap.LayGioHang.KhoiTaoGioHang();
                Load_GioHang();
                Session["TongTien"] = hd_LayVe.TongTien;
            }
        }

        protected void btn_DongYMua_Click(object sender, EventArgs e)
        {
            ThongTinTaiKhoan tt = new ThongTinTaiKhoan();
            tt.DiaChi = txt_DiaChi.Text;
            tt.Nickname = txt_Email.Text;
            tt.DienThoai = txt_SDT.Text;
            tt.HoTen = txt_HoTen.Text;
            if (check_TanNha.Checked == true)
            {
                LapThanhToan();
            }
            else
            {
                    if (check_ATM.Checked == false && check_TanNha.Checked == false)
                    {
                        LapThanhToan();
                    }
                }
            }
        }
    }